<?php
require_once('../../includes/classes/core.php');
$a=mysql_fetch_array(mysql_query("SELECT `data`,`data1` FROM `client_invoise_form` WHERE client_id=$_REQUEST[id] ORDER BY id DESC LIMIT 1") );
?>

<!DOCTYPE meta PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
</head>

	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	 <style type="text/css">
table, td, th {
padding: 0 5px;
border-collapse: collapse;
border: 1px solid #000;
text-align: center;
  }
 td:nth-child(5){
text-align: right;
  }
    </style>
<body style="width: 21cm">

<?php
echo "$a[0]";
//-------------------------------თი ბი სი------------------------------
if ($_REQUEST[id]==11) {
$rResult = mysql_query("
        SELECT
		        production.`name`,
				IF(b_letter_details.part_type_id=2,'დამუხტვა',CONCAT(cartridge_parts_type.`name`, 'ს შეცვლა')) AS `part`,
				'ცალი',
		        COUNT(*),
		        sum(IF(warehouse_detail.type=2 AND b_letter_details.part_type_id=2,0.00,b_letter_details.price))  AS tax,
            SUBSTRING(client_objects.`name`, 1, 9)
		FROM    `invoice`
		JOIN   a_letter ON a_letter.invoice_id = invoice.id
		JOIN   b_letter_details ON b_letter_details.a_letter_id=a_letter.id
		JOIN   client_objects ON a_letter.client_object_id = client_objects.id
		JOIN   warehouse_detail ON b_letter_details.warehouse_detail_id = warehouse_detail.id AND warehouse_detail.actived=1 AND warehouse_detail.type !=2
		JOIN 	 production ON production.id=warehouse_detail.prod_id
        JOIN   cartridge_parts_type ON cartridge_parts_type.id = b_letter_details.part_type_id
		WHERE  invoice_id=$_REQUEST[inv_id]
		GROUP BY IF(SUBSTRING(client_objects.`name`, 1, 9)='კონსტანტა','1','0'), production.id, cartridge_parts_type.id
        HAVING tax>0
		UNION ALL
		SELECT 	production.`name`,
		        printer_parts_type.`name` AS `part`,
				'ცალი',
		        COUNT(*),
		        sum(e_letter_details.price)  AS tax,
            SUBSTRING(client_objects.`name`, 1, 9)
		FROM    `invoice`
		JOIN   d_letter ON d_letter.invoice_id = invoice.id
		JOIN   e_letter_details ON e_letter_details.d_letter_id=d_letter.id
		JOIN   client_objects ON d_letter.client_object_id = client_objects.id
        JOIN   warehouse_detail ON e_letter_details.warehouse_detail_id = warehouse_detail.id AND warehouse_detail.actived=1 AND warehouse_detail.type !=2
		JOIN 	 production ON production.id=warehouse_detail.prod_id
		JOIN   printer_parts_type ON printer_parts_type.id = e_letter_details.part_type_id
		WHERE  invoice_id=$_REQUEST[inv_id]
		GROUP BY IF(SUBSTRING(client_objects.`name`, 1, 9)='კონსტანტა','1','0'), production.id, printer_parts_type.id
        HAVING tax>0 
    ");
?>
<table style="margin: auto; width: 20cm;">
  <tr>
    <th>კარტრიჯის მოდელი</th>
    <th>მომსახურების სახე</th>
    <th>ერთეული</th>
    <th>რაოდენობა</th>
    <th>ფასი</th>
  </tr>
<?php
$tbc = array();
$con = array();
    while ( $aRow = mysql_fetch_array( $rResult ) )
    {
        if ($aRow[5]=='კონსტანტა') {
            if ($aRow[1]=='დამუხტვა') {
                $con[cartrije].= "<tr><td>$aRow[0]</td><td>$aRow[1]</td><td>$aRow[2]</td><td>$aRow[3]</td><td>$aRow[4]</td></tr>";
                $con[c_sum]+=$aRow[4];
                }else {
                $con[printer].= "<tr><td>$aRow[0]</td><td>$aRow[1]</td><td>$aRow[2]</td><td>$aRow[3]</td><td>$aRow[4]</td></tr>";
                $con[p_sum]+=$aRow[4];
                }
        }else {
            if ($aRow[1]=='დამუხტვა') {
                $tbc[cartrije].= "<tr><td>$aRow[0]</td><td>$aRow[1]</td><td>$aRow[2]</td><td>$aRow[3]</td><td>$aRow[4]</td></tr>";
                $tbc[c_sum]+=$aRow[4];
            }else {
                $tbc[printer].= "<tr><td>$aRow[0]</td><td>$aRow[1]</td><td>$aRow[2]</td><td>$aRow[3]</td><td>$aRow[4]</td></tr>";
                $tbc[p_sum]+=$aRow[4];
            }
        }
    }
 echo $tbc[cartrije];
?>
</table>
<div style="padding: 5px 19px 50px 0px; text-align: right;"> ჯამი :

<?php echo number_format((float)$tbc[c_sum], 2, '.', '');?>
</div>
<br/>
<br/>
<br/>
<br/>
<br/>
<div style="page-break-inside : avoid;">
<table style="margin: auto; width: 20cm;">
  <tr>
    <th>პრინტერის მოდელი</th>
    <th>მომსახურების სახე</th>
    <th>ერთეული</th>
    <th>რაოდენობა</th>
    <th>ფასი</th>
  </tr>
<?php
echo $tbc[printer];
?>
</table>
<div style="padding: 5px 19px 50px 0px; text-align: right;"> ჯამი :
<?php echo number_format((float)$tbc[p_sum], 2, '.', '');?>
</div>
</div>
<br/>
<br/>
<br/>
<br/>
<br/>
<div style="page-break-inside : avoid;">
<div style="text-align: center; page-break-inside : avoid;  border: 1px solid #000;   border-bottom: rgba(210, 180, 140, 0);   margin: 0 0 0 19px;   width: 224px;">კონსტანტას კარტრიჯები</div>
<table style="margin: auto; width: 20cm;">
  <tr>
    <th>კარტრიჯის მოდელი</th>
    <th>მომსახურების სახე</th>
    <th>ერთეული</th>
    <th>რაოდენობა</th>
    <th>ფასი</th>
  </tr>
<?php
echo $con[cartrije];
?>
</table>
<div style="padding: 5px 19px 50px 0px; text-align: right;"> ჯამი :

<?php echo number_format((float)$con[c_sum], 2, '.', '');?>
</div>
</div>
<br/>
<br/>
<br/>
<br/>

<br/>
<div style="page-break-inside : avoid;">
<div style="text-align: center; page-break-inside : avoid;  border: 1px solid #000;   border-bottom: rgba(210, 180, 140, 0);   margin: 0 0 0 19px;   width: 224px;">კონსტანტას პრინტერები</div>
<table style="margin: auto; width: 20cm;">
  <tr>
    <th>პრინტერის მოდელი</th>
    <th>მომსახურების სახე</th>
    <th>ერთეული</th>
    <th>რაოდენობა</th>
    <th>ფასი</th>
  </tr>
<?php
echo $con[printer];
?>
</table>

<div style="padding: 5px 19px 50px 0px; text-align: right; "> ჯამი :
<?php echo number_format((float)(float)$con[p_sum],2, '.', '');?>
</div>
</div>
<div style="padding: 20px; font-size:20px; text-align: right; width: 90%;">საერთო ჯამი :
<?php echo number_format((float)(float)$con[c_sum]+$con[p_sum]+$tbc[p_sum]+$tbc[c_sum],2, '.', '');?>
</div>
<?php
}
/////////-------------------------------რესპუბლიკა------------------------------
elseif ($_REQUEST[id]==12){
    $rResult = mysql_query("
            SELECT  client_objects.`name`,
		        production.`name`,
						IF(b_letter_details.part_type_id=2,'დამუხტვა',CONCAT(cartridge_parts_type.`name`, 'ს შეცვლა')) AS `part`,
						'ცალი',
		        COUNT(*),
						b_letter_details.price,
		        sum(IF(warehouse_detail.type=2 AND b_letter_details.part_type_id=2,0.00,b_letter_details.price))  AS tax            
		FROM    `invoice`
		JOIN   a_letter ON a_letter.invoice_id = invoice.id
		JOIN   b_letter_details ON b_letter_details.a_letter_id=a_letter.id
		JOIN   client_objects ON a_letter.client_object_id = client_objects.id
		JOIN   warehouse_detail ON b_letter_details.warehouse_detail_id = warehouse_detail.id AND warehouse_detail.actived=1 AND warehouse_detail.type !=2
		JOIN 	 production ON production.id=warehouse_detail.prod_id
    JOIN   cartridge_parts_type ON cartridge_parts_type.id = b_letter_details.part_type_id
		WHERE  invoice_id=$_REQUEST[inv_id]
		GROUP BY IF(SUBSTRING(client_objects.`name`, 1, 9)='კონსტანტა','1','0'), production.id, cartridge_parts_type.id
        HAVING tax>0
		UNION ALL
		SELECT 	client_objects.`name`,
						production.`name`,
		        printer_parts_type.`name` AS `part`,
						'ცალი',
		        COUNT(*),
						e_letter_details.price,
		        sum(e_letter_details.price)  AS tax
		FROM    `invoice`
		JOIN   d_letter ON d_letter.invoice_id = invoice.id
		JOIN   e_letter_details ON e_letter_details.d_letter_id=d_letter.id
		JOIN   client_objects ON d_letter.client_object_id = client_objects.id
    JOIN   warehouse_detail ON e_letter_details.warehouse_detail_id = warehouse_detail.id AND warehouse_detail.actived=1 AND warehouse_detail.type !=2
		JOIN 	 production ON production.id=warehouse_detail.prod_id
		JOIN   printer_parts_type ON printer_parts_type.id = e_letter_details.part_type_id
		WHERE  invoice_id=$_REQUEST[inv_id]
		GROUP BY IF(SUBSTRING(client_objects.`name`, 1, 9)='კონსტანტა','1','0'), production.id, printer_parts_type.id
        HAVING tax>0     ");
    ?>
    <table style="margin: auto; width: 21cm;">
      <tr>
        <th>ფილიალი</th>
        <th>მოდელი</th>
        <th>მომსახურების სახე</th>
        <th>ერთეული</th>
        <th>რ. ბა</th>
        <th>ერთ. ფასი</th>
        <th>ფასი</th>
      </tr>
    <?php
    $i=0;
    while ( $aRow = mysql_fetch_array( $rResult ) )
    {
                $cartrije.= "<tr><td>$aRow[0]</td><td>$aRow[1]</td><td>$aRow[2]</td><td>$aRow[3]</td><td>$aRow[4]</td><td>$aRow[5]</td><td>$aRow[6]</td></tr>";
                $sum+=$aRow[tax];
                $i++;
                if ($i==25) {
                    $cartrije.="</table><br /><br /><br /><br /><br /><br /><table style='margin: auto; width: 21cm;'>
	                             <tr>
                                        <th>ფილიალი</th>
                                        <th>მოდელი</th>
                                        <th>მომსახურების სახე</th>
                                        <th>ერთეული</th>
                                        <th>რ. ბა</th>
                                        <th>ერთ. ფასი</th>
                                        <th>ფასი</th>
                                  </tr>";
                }
    }

     echo $cartrije;

    ?>
    </table>
    <div style="font-size:16px; text-align: right; width: 90%;">საერთო ჯამი :
    <?php echo number_format((float)$sum,2, '.', '');?>
    </div>
    <?php
}
/////////-------------------------------konstanta------------------------------
elseif ($_REQUEST[id]==14){
    $rResult = mysql_query("SELECT	CASE
				WHEN  SUBSTRING_INDEX(`invoice_detail`.`service`,' ',-1)='დამუხტვა' THEN 	'კარტრიჯი'
				WHEN  SUBSTRING_INDEX(`invoice_detail`.`service`,' ', 1)='Nex' OR SUBSTRING_INDEX(`invoice_detail`.`service`,' ', 1)='talaris' THEN 	'ფულის სათვლელი'
				ELSE 'პრინტერები'
				END as d,
				SUBSTRING_INDEX(`invoice_detail`.`service`,' ',-1) AS service1,
                unit,
				sum(`invoice_detail`.`amount`),
				sum(`invoice_detail`.`tax`) as tax
FROM	`invoice_detail`
WHERE	`invoice_detail`.`invoice_id` = '$_REQUEST[inv_id]'
GROUP BY d

        ");
        ?>
    <table style="margin: auto; width: 20cm;">
      <tr>
        <th>დასახელება</th>
        <th>შესრულებული სამუშაო</th>
        <th>რაოდენობა</th>
        <th>ღირებულება</th>
      </tr>
    <?php
    while ( $aRow = mysql_fetch_array( $rResult ) )
    {
                $cartrije.= "<tr><td>$aRow[0]</td><td>$aRow[1]</td><td>$aRow[2]</td><td>$aRow[3]</td></tr>";
                $sum+=$aRow[tax];
    }

     echo $cartrije;

    ?>
    </table>
    <div style="padding: 20px; font-size:20px; text-align: right; width: 90%;">საერთო ჯამი :
    <?php echo number_format((float)$sum,2, '.', '');?>
    </div>
    <?php
}
else {
    $rResult = mysql_query("
SELECT	SUBSTRING_INDEX(`invoice_detail`.`service`,' - ',1),
		REPLACE(REPLACE(`invoice_detail`.`service`, SUBSTRING_INDEX(`invoice_detail`.`service`,' - ',1), ''),' - ',''),
        invoice_detail.unit,
		`invoice_detail`.`amount`,
		ROUND(tax/amount,2),
		`invoice_detail`.`tax`
FROM	`invoice_detail`
WHERE	`invoice_detail`.`invoice_id` = '$_REQUEST[inv_id]'");
    ?>  <table style="margin: auto; width: 20cm;">
          <tr>
            <th>მოდელი</th>
            <th>მომსახურების სახე</th>
            <th>ერთეული</th>
            <th>რაოდენობა</th>
            <th>ერთეულის  <br> ფასი</th>
            <th>ფასი</th>
          </tr>
        <?php
        $i=0;
        while ( $aRow = mysql_fetch_array( $rResult ) )
        {
                    $cartrije.= "<tr><td>$aRow[0]</td><td>$aRow[1]</td><td>$aRow[2]</td><td>$aRow[3]</td><td>$aRow[4]</td><td>$aRow[5]</td></tr>";
                    $sum+=$aRow[tax];
                    $i++;
                    if ($i==15) {
                        $cartrije.="</table><br /><br /><br /><br /><p><br></p><p><br></p><br /><table style='margin: auto; width: 20cm;'>
	                                             <tr>
                                    <th>მოდელი</th>
                                    <th>მომსახურების სახე</th>
                                    <th>ერთეული</th>
                                    <th>რაოდენობა</th>
	                                <th>ერთეულის  <br> ფასი</th>
                                    <th>ფასი</th>
                                  </tr>";
                    }

        }

         echo $cartrije;

        ?>
        </table>
        <div style="padding: 20px; font-size:20px; text-align: right; width: 90%;">საერთო ჯამი :
        <?php echo number_format((float)$sum,2, '.', '');?>
        </div>
        <?php
}
echo $a[1]
?>
</body>
</html>